package com.lmk.com.lmk.sc.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import com.lmk.sc.entity.User;

@RestController
public class UserController {

    // 拼接服务提供者访问接口
    String ProviderUrl = "http://UserProvider/api/user";

    @Autowired
    RestTemplate restTemplate;

    @GetMapping("/consumer/user")
    public List<User> list(){
        List<User> userList = restTemplate.getForObject(ProviderUrl, List.class);
        return userList;
    }

    @GetMapping("/consumer/user/{id}")
    public User get(@PathVariable("id") Integer id){
        User user = restTemplate.getForObject(ProviderUrl + "/" + id, User.class);
        return user;
    }

    @DeleteMapping("/consumer/user/{id}")
    public Map<String, Object> delete(@PathVariable("id") Integer id){
        restTemplate.delete(ProviderUrl + "/" + id);

        Map<String, Object> result = new HashMap<>();
        result.put("status", "ok");
        return result;
    }

    @PostMapping("/consumer/user")
    public Map<String, Object> post(@RequestBody User user){
        Map<String, Object> result =  restTemplate.postForObject(ProviderUrl, user, Map.class);
        return result;
    }

    @PutMapping("/consumer/user")
    public Map<String, Object> put(@RequestBody User user){
        restTemplate.put(ProviderUrl, user);

        Map<String, Object> result = new HashMap<>();
        result.put("status", "ok");
        return result;
    }
}